package com.house.rent.ui.fragment;

import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.base.lib.mvp.present.BasePresenter;
import com.house.rent.R;
import com.house.rent.entity.HouseEntity;
import com.house.rent.ui.activity.HouseDetailActivity;
import com.house.rent.ui.adapter.HouseAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @date 2019-10-23 15:05
 * @desc 房源页
 */
public class HouseFragment extends BaseFragment {

    private static List<HouseEntity> mHouseEntities = new ArrayList<>();

    @BindView(R.id.refresh_home)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.rcv_house)
    RecyclerView mRecyclerView;

    HouseAdapter mAdapter;
    int mType;

    public static HouseFragment newInstance(int type) {
        HouseFragment fragment = new HouseFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initTop() {
        enableTop(false);
    }

    @Override
    protected void initView() {
        mType = getArguments().getInt("type", 0);

        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.colorPrimary));

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter = new HouseAdapter(mContext));
    }

    @Override
    protected void initListeners() {
        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            sendRequest("house-info/list", HouseEntity.class);
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            HouseDetailActivity.start(mContext, mAdapter.getData().get(position).getId());
        });
        mAdapter.setOnLoadMoreListener(null, mRecyclerView);
        mAdapter.setEmptyView(R.layout.layout_empty);
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {
        if (mHouseEntities.isEmpty()) {
            sendRequest("house-info/list", HouseEntity.class);
        } else {
            // 类型 0-出售 1-求购 2-求租 3-出租
            List<HouseEntity> list = new ArrayList<>();
            for (HouseEntity entity : mHouseEntities) {
                if (mType == entity.getType()) {
                    list.add(entity);
                }
            }

            mAdapter.setNewData(list);
            mAdapter.loadMoreEnd();
        }
    }

    @Override
    protected void onRequestSuccess(String reqId, Object obj) {
        super.onRequestSuccess(reqId, obj);
        if (obj instanceof List) {
            List<HouseEntity> entities = (List<HouseEntity>) obj;
            mHouseEntities.clear();
            mHouseEntities.addAll(entities);

            // 类型 0-出售 1-求购 2-求租 3-出租
            List<HouseEntity> list = new ArrayList<>();
            for (HouseEntity entity : mHouseEntities) {
                if (mType == entity.getType()) {
                    list.add(entity);
                }
            }

            mAdapter.setNewData(list);
            mAdapter.loadMoreEnd();
        }
    }

    @Override
    public void showLoading(String message) {
        if (!mSwipeRefreshLayout.isRefreshing()) {
            super.showLoading(message);
        }
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        if (mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mHouseEntities.clear();
    }
}
